#!/usr/local/bin/php
<?php

require_once 'vendor/autoload.php';

$laravelDir = __DIR__;

info("Laravel directory: $laravelDir");

if (!file_exists("$laravelDir/routes")) {
    info('Error: not Laravel directory');
    exit;
}

if (!file_exists("$laravelDir/.env")) {
    info('Error: .env file not created, please create .env file manually');
    exit;
}

info("Copy vars/docs/* to public/docs/");
$varsDocsDir = "$laravelDir/vars/docs";
$publicDocsDir = "$laravelDir/public/docs";
if (!file_exists($publicDocsDir)) {
    mkdir($publicDocsDir, 0777, true);
}
$docFiles = array_filter(scandir($varsDocsDir), function ($item) {
    return !startsWith($item, '.');
});
foreach ($docFiles as $docFile) {
    info("  Copying $docFile");
    $src = "$varsDocsDir/$docFile";
    $dst = "$publicDocsDir/$docFile";
    copyFile($src, $dst);
}
info("OK");

function copyFile($src, $dst)
{
    if (isWindows()) {
        info('TODO: Copy files on Windows (charset issue)');
        exit;
    } else {
        copy($src, $dst);
    }
}

function info($message)
{
    $now = date('Y-m-d H:i:s');
    echo "[$now] $message\n";
}

function isWindows()
{
    return stripos(PHP_OS, 'WIN') === 0;
}

function startsWith($string, $startString)
{
    $len = strlen($startString);
    return (substr($string, 0, $len) === $startString);
}